package com.amuse.amuse.service.impl;

import com.amuse.amuse.entity.*;
import com.amuse.amuse.entity.clubAnnouncements;
import com.amuse.amuse.entity.clubApplications;
import com.amuse.amuse.entity.clubMembers;
import com.amuse.amuse.entity.clubs;
import com.amuse.amuse.mapper.club_permissions.ClubPermissionsDao;
import com.amuse.amuse.service.ClubPermissionsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ClubPermissionsServiceImpl implements ClubPermissionsService {
@Autowired
private ClubPermissionsDao clubPermissionsDao;
//    // 获取日志记录器
    private static final Logger logger = LoggerFactory.getLogger(log.class);
    // 获取当前时间
    LocalDateTime now = LocalDateTime.now();

    // 定义日期时间格式器
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss");
    // 格式化日期时间
    private String publishDate = now.format(formatter);




    /*
    * 社长添加俱乐部公告
    * */
    @Override
    public Integer addClubAnnouncements(String announcementId,Integer clubId,String content,String publishDate,String title) {
            // 获取日志记录器

        //判断输入的公告id是否为社团名+月+日拼接起来的字符串
                // 格式化日期时间
                publishDate = this.publishDate;
                List<clubMembers> clubMembers = clubPermissionsDao.findClubMembers();
                if (clubId == null || content == null || title == null) {
                    logger.error("输入参数不合法");
                    return 0;
                }
                for (clubMembers clubMember : clubMembers){
                    if (clubMember.getRoleId() == null){
                        continue;
                    }
                    if (clubMember.getClubId().equals(clubId) && clubMember.getRoleId() == 1) {
                        announcementId = (clubId+publishDate.substring(5, 7) + publishDate.substring(8, 10) + publishDate.substring(11, 13) + publishDate.substring(14, 16));
                        return clubPermissionsDao.addClubAnnouncements(announcementId, clubId, content, publishDate, title);
                    }else {
                        logger.error("你没有权限");
                        return 0;
                    }
                }
                return null;

    }

    //社长删除公告
    @Override
    public Integer deleteClubAnnouncements(String announcementId, Integer clubId) {
        //判断是哪个社以及是不是社长
        List<clubMembers> clubMembers = clubPermissionsDao.findClubMembers();
        if (announcementId == null || clubId == null) {
            logger.error("输入参数不合法");
            return 0;
        }
        for (clubMembers clubMember : clubMembers) {
            if (clubMember.getRoleId() == null){
                continue;
            }
            if (clubMember.getClubId().equals(clubId) && clubMember.getRoleId() == 1) {
                    return clubPermissionsDao.deleteClubAnnouncements(announcementId, clubId);
            }else {
                logger.error("你没有权限");
                return 0;
            }
        }
        return 0;
    }

    /*
    * 社长更新公告
    * */
    @Override
    public Integer updateClubAnnouncements(Integer clubId,String announcementId, String title, String content, String publishDate) {
        if (title == null || clubId == null || announcementId == null || publishDate == null || content == null) {
            logger.error("输入参数不合法");
            return 0;
        }
        //判断是哪个社以及是不是社长
        List<clubMembers> clubMembers = clubPermissionsDao.findClubMembers();
        for (clubMembers clubMember : clubMembers){
            if (clubMember.getRoleId() == null){
                continue;
            }
            if (clubMember.getClubId().equals(clubId) && clubMember.getRoleId() == 1){
                return clubPermissionsDao.updateClubAnnouncements(clubId,announcementId, title, content, this.publishDate);
            }else {
                logger.error("你没有权限");
                return 0;
            }
        }
        return null;
    }

    /*
    * 社长更新社团描述
    * */

    @Override
    public Integer updateClubDescription(Integer clubId, String description) {
        //判断是哪个社以及是不是社长
        List<clubMembers> clubMembers = clubPermissionsDao.findClubMembers();
        for (clubMembers clubMember : clubMembers){
            if (clubMember.getRoleId() == null){
                continue;
            }
            if (clubMember.getClubId().equals(clubId) && clubMember.getRoleId() == 1){
                return clubPermissionsDao.updateClubDescription(clubId,description);
            }else {
                logger.error("你没有权限");
                return 0;
            }
        }
        return null;
    }

    /*
    * 社长学生查看公告
    * */
    @Override
    public PageBean<clubAnnouncements> findClubAnnouncements(Integer pageNum, Integer pageSize, Integer clubId) {
        PageBean<clubAnnouncements> clubAnnouncementsPageBean = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<clubAnnouncements> clubAnnouncements = clubPermissionsDao.findClubAnnouncements(clubId);
        Page<clubAnnouncements> page = (Page<clubAnnouncements>)clubAnnouncements;
        clubAnnouncementsPageBean.setItems(page.getResult());
        clubAnnouncementsPageBean.setTotal(page.getTotal());

        return clubAnnouncementsPageBean;
    }

    /*
    * 学生，社长查看社团信息，查询结果分页显示
    * */
    @Override
    public PageBean<clubs> findClubsInfo(Integer pageNum, Integer pageSize, String clubName) {
        // 初始化分页对象，用于存储查询结果和分页信息
        PageBean<clubs> clubAnnouncementsPageBean = new PageBean<>();

        // 开始分页查询，指定页码和每页记录数
        PageHelper.startPage(pageNum, pageSize);
        // 根据社团名字查询信息
        List<clubs> clubsInfo = clubPermissionsDao.findClubsInfo(clubName);
        // 将查询结果转换为分页对象
        Page<clubs> page = (Page<clubs>)clubsInfo;
        // 设置分页对象的结果集和总记录数
        clubAnnouncementsPageBean.setItems(page.getResult());
        clubAnnouncementsPageBean.setTotal(page.getTotal());
        // 返回分页对象
        return clubAnnouncementsPageBean;

    }

    /*
    * 学生申请加入社团
    * */
    @Override
    public Integer StudentsApplyToClub(Integer clubId, Integer studentId) {
        //判断学生是否加入该社团
        List<clubMembers> clubMembers = clubPermissionsDao.findClubMembers();
        if (studentId == null || clubId == null) {
            logger.error("输入参数不合法");
            return 0;
        }
        /*
        * 对社团成员进行增强循环，查询学生输入的社团id和学生id是否在社团成员中存在
        * */
        for (clubMembers clubMember : clubMembers){
            if (clubMember.getStudentId() == null || clubMember.getClubId() == null){
                continue;
            }
          if (clubMember.getClubId().equals(clubId) && clubMember.getStudentId().equals(studentId)) {
              logger.error("您已加入社团");
              return 0;
          }else {
              //判断学生是否已经提交申请
              List<clubApplications> clubApplications1 = clubPermissionsDao.findClubApplications1();
              for (clubApplications clubApplication : clubApplications1){
                  if ( clubApplication.getStudentId() == null || clubApplication.getClubId() == null){
                      continue;
                  }
                  if (clubApplication.getStudentId().equals(studentId) && clubApplication.getClubId().equals(clubId)){
                      logger.error("您已提交申请，请勿重复提交");
                      return 0;
                  }}
              logger.error("您已提交申请，请等待审批");
              return clubPermissionsDao.StudentsApplyToClub(clubId,studentId);
          }
        }
        return null;
    }

    /*
    * 更新学生申请状态
    * */
    @Override
    public Integer updateClubApplications(Integer status, Integer studentId, Integer clubId) {
        /*
        * 社长更新学生的申请状态，如果为1，则更新学生的申请状态为1，否则不更新
        * */
        if (status == null || studentId == null || clubId == null){
            logger.error("输入参数不合法");
            return 0;
        }

        return clubPermissionsDao.updateClubApplications(status,studentId,clubId);


    }


    /*
     * 社长添加社团成员
     * */
    @Override
    public Integer addClubMembers(Integer clubId) {
        /*
        * 如果学生申请表中的状态为1，则添加社团成员，否则不添加
        * */
        List<clubApplications> clubApplications = clubPermissionsDao.findClubApplications(clubId);


        for (clubApplications clubApplication : clubApplications){
            if (clubApplication.getClubId() == null || clubApplication.getStudentId() == null || clubApplication.getStatus() == null){
                continue;
            }
            //判断学生是否已经加入该社团
            List<clubMembers> clubMembers = clubPermissionsDao.findClubMembers();
            for (clubMembers clubMember : clubMembers){
                if (clubMember.getStudentId() == null || clubMember.getClubId() == null){
                    continue;
                }
                if (clubMember.getClubId().equals(clubApplication.getClubId()) && clubMember.getStudentId().equals(clubApplication.getStudentId())){
                    logger.error("您已加入社团");
                    return 0;
                }
            }
            if (clubApplication.getStatus() == 1){
                logger.error("成功加入社团");
                return clubPermissionsDao.addClubMembers(clubApplication.getClubId(), clubApplication.getStudentId());
            }else {
                logger.error("加入社团失败");
                return 0;
            }
        }
        return null;
    }



    /*
    * 社长查看学生申请状态
    * */
    @Override
    public PageBean<clubApplications> findClubApplications(Integer pageNum, Integer pageSize,Integer clubId) {
        PageBean<clubApplications> clubApplications =  new PageBean<>();
        PageHelper.startPage(1, 10);  //PageHelper是Mybatis提供的一个分页插件，它通过拦截MyBatis的执行过程，在执行SQL之前自动添加LIMIT和OFFSET（或SQL Server、Oracle等数据库对应的分页语法）到原始SQL中，从而实现分页查询。

        List<clubApplications> clubApplications1 = clubPermissionsDao.findClubApplications(clubId);
        Page<clubApplications> page = (Page<clubApplications>)clubApplications1;
        clubApplications.setItems(page.getResult());
        clubApplications.setTotal(page.getTotal());
        return clubApplications;
    }

    /*
    * 社长学生查看成员容量
    * */
    @Override
    public clubs getCurrentMember(Integer clubId) {
        clubPermissionsDao.updateCurrentMember(clubId,clubPermissionsDao.clubMembersCount(clubId));
        return clubPermissionsDao.getCurrentMember(clubId);
    }
}
